package com.wefans.wefansapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.Toast;

import com.wefans.wefansapp.R;
import com.wefans.wefansapp.common.tool.data.MFDataManager;
import com.wefans.wefansapp.common.tool.data.UrlInfo;
import com.wefans.wefansapp.common.tool.debug.MFDebug;
import com.wefans.wefansapp.common.tool.popup.SaveStarDialog;

public class NotificationActivity extends BaseActivity {

	private DrawerLayout   m_DrawerLayout  = null;
    private FrameLayout    m_DrawerView    = null;
    private WebView        m_Notification  = null;
    private MFDataManager    m_oDataManager   = null;
    private Toast toastView;
	
    
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_notification);
		initialize();
		installEvent();
		
	}
	
	@Override
    protected void onPause()
    {
        MFDebug.d(">> onPause()");
        
        notifyDoClaseDrawerMenu();
        
        super.onPause();
    }

    @Override
    protected void notifyDoClaseDrawerMenu()
    {
        MFDebug.d(">> notifyDoClaseDrawerMenu()");
        
        super.notifyDoClaseDrawerMenu();
        
        if( m_DrawerLayout == null ) {
            MFDebug.e(new Exception("There are NO Drawer-Layout"));
            return;
        }
        
        if( m_DrawerView == null ) {
            MFDebug.e(new Exception("There are NO Drawer-View"));
            return;
        }
        
        m_DrawerLayout.closeDrawer(m_DrawerView);
        m_sdQuickMenu.close();
    }
	
	@Override
    public void onClick(View v)
    {
        MFDebug.d(">> onClick()");
        
        switch( v.getId() ) {
            case R.id.IB_NOTIFICATION_HEADER_LEFT_BUTTON :
                m_DrawerLayout.openDrawer(m_DrawerView);
                break;
            case R.id.IB_NOTIFICATION_HEADER_RIGHT_BUTTON :
            	MFDebug.d("++ v.getId() : " + "IB_BENEFIT_HEADER_RIGHT_BUTTON");
				Intent it = new Intent(getApplicationContext(), MainActivity.class);
				startActivityForResult(it,v.getId());
                break;
            default :
                break;
        }
        
        super.onClick(v);
    }
	
    private void initialize()
    {
    	 MFDebug.d(">> initialize()");
         
         //Contents layout adding
         FrameLayout containerContents = (FrameLayout) findViewById(R.id.FL_NOTIFICATION_CONTAINER);
         addContentsLayout(containerContents, R.layout.layout_notification);
         addQuickMenuLayout(containerContents);
         
         //Drawer menu adding
         m_DrawerView = (FrameLayout) findViewById(R.id.FL_NOTIFICATION_DRAWER_LAYOUT);
         addDrawerLayout(m_DrawerView);
         
         //set drawer layout (root)
         m_DrawerLayout = (DrawerLayout) findViewById(R.id.DL_NOTIFICATION_NAVIGATION_LAYOUT);
    }
    
    private void installEvent()
    {
 		
    	((ImageButton) findViewById(R.id.IB_NOTIFICATION_HEADER_LEFT_BUTTON)).setOnClickListener(this);
    	((ImageButton) findViewById(R.id.IB_NOTIFICATION_HEADER_RIGHT_BUTTON)).setOnClickListener(this);
    	m_Notification = (WebView) findViewById(R.id.WV_NOTIFICATION);
    	//m_Notification.loadUrl("http://m.wefansapp.com/breaktime/notice_list.php");
    	// 웹뷰에서 자바스크립트실행가능
    	m_Notification.getSettings().setJavaScriptEnabled(true); 
    	m_oDataManager   = MFDataManager.getInstance(this);
    	m_Notification.loadUrl("http://m.wefansapp.com/breaktime/notice_list.php?mem_idx="+m_oDataManager.getMebmerIdx());
        // WebViewClient 지정
    	m_Notification.setWebViewClient(new WebViewClientClass());  
        
    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
        if ((keyCode == KeyEvent.KEYCODE_BACK) && m_Notification.canGoBack()) { 
        	Intent it = new Intent(getApplicationContext(), MainActivity.class);
			startActivity(it);
			finish();
            return true; 
        } 
        return super.onKeyDown(keyCode, event);
    }
     
    private class WebViewClientClass extends WebViewClient { 
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) { 
            view.loadUrl(url); 
            return true; 
        } 
    }
}